package com.spring.security.web.service.impl;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.spring.security.web.service.RedisDataSource;

import redis.clients.jedis.JedisPool;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

@Service
public class RedisDataSourceImpl implements RedisDataSource {
	private static final Logger log = Logger.getLogger(RedisDataSourceImpl.class.getName());

//	@Resource
//	private JedisPool jedisPool;
	
	@Autowired
	private ShardedJedisPool shardedJedisPool;

	public ShardedJedis getRedisClient() {
		try {
//			jedisPool.getResource();
			System.out.println("--------->pool:" + shardedJedisPool);
			ShardedJedis shardJedis = shardedJedisPool.getResource();
			System.out.println("------------->resource:" + shardJedis);
			return shardJedis;
		} catch (Exception e) {
			log.error("getRedisClent error", e);
		}
		return null;
	}

	@SuppressWarnings("deprecation")
	public void returnResource(ShardedJedis shardedJedis) {
		shardedJedisPool.returnResource(shardedJedis);
	}

	@SuppressWarnings("deprecation")
	public void returnResource(ShardedJedis shardedJedis, boolean broken) {
		if (broken) {
			shardedJedisPool.returnBrokenResource(shardedJedis);
		} else {
			shardedJedisPool.returnResource(shardedJedis);
		}
	}
}
